# -*- coding: utf-8 -*-
import unittest
import os, sys


parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

from fixture import user

from api.base_api.base_api import BaseApi
from api.base_api.register import RegisterApi



class UserRegisterTest(BaseApi):
    ''' 测试注册 '''

    def setUp(self):
        self.mobile = '13381078657'
        self.password = '123abc'
        self.again_mobile = '13651020524'

    def tearDown(self):
        user.clear_user(mobile=self.mobile)

    def test_user_register_access(self):
        ''' 注册成功 '''
        verify_code = RegisterApi().get_user_verify_code(self.mobile)
        self.response = RegisterApi().register(self.mobile, verify_code, self.password)
        status_code = self.response.status_code
        self.assertEqual(status_code, 200)

        token = self.response.json()['token']
        self.assertIsNotNone(token)

        db_user_name = user.select_user(self.mobile)
        self.assertEqual(db_user_name, self.mobile)

    def test_user_again_register(self):
        ''' 手机号已存在 '''
        verify_code = RegisterApi().get_user_verify_code(self.again_mobile)
        self.response = RegisterApi().register(self.again_mobile, verify_code, self.password)
        status_code = self.response.status_code
        self.assertNotEqual(status_code, 200)
        message = self.response.json()['message']
        self.assertEqual(message, '手机号已存在')


if __name__ == '__main__':
    #用例默认执行顺序是0-9,a-z,所以先执行test_user_again_register
    #构造测试集
    suite = unittest.TestSuite()
    suite.addTest(UserRegisterTest("test_user_register_access"))
    suite.addTest(UserRegisterTest("test_user_verify_code_fail"))
    suite.addTest(UserRegisterTest("test_user_again_register"))

    #执行测试
    runner = unittest.TextTestRunner()
    runner.run(suite)
    # unittest.main()
